package com.zrx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zrx.domain.ResponseResult;
import com.zrx.domain.dto.TagListDto;
import com.zrx.domain.entity.Tag;
import com.zrx.domain.vo.PageVo;
import com.zrx.domain.vo.TogVo;
import com.zrx.mapper.TagMapper;
import com.zrx.service.TagService;
import com.zrx.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2022-09-26 10:01:19
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    /**
     * 分页查询文字标签
     * @param pageNum
     * @param pageSize
     * @param tagListDto
     * @return
     */
    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //分页查询
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(tagListDto.getName()),Tag::getName,tagListDto.getName());
        queryWrapper.eq(StringUtils.hasText(tagListDto.getRemark()),Tag::getRemark,tagListDto.getRemark());

        Page<Tag> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);

        //封装数据
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());


        return ResponseResult.okResult(pageVo);
    }

    /**
     * 查询标签
     * @return
     */
     @Override
    public ResponseResult listAlltag() {
         LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.select(Tag::getId,Tag::getName);
         List<Tag> list = list(queryWrapper);
         List<TogVo> togVos = BeanCopyUtils.copyBeanList(list, TogVo.class);
         return ResponseResult.okResult(togVos);
    }
}

